
package com.rasenhund.android.paintstock;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.SimpleExpandableListAdapter;

import com.rasenhund.android.paintstock.dao.SeriesDao;
import com.rasenhund.android.paintstock.schema.Paint;
import com.rasenhund.android.paintstock.schema.Series;

/**
 * シリーズから探す
 */
public class SeriesListFragment extends Fragment {

    // ExpandableList レイアウト指定用キー
    private final String KEY_TITLE = "title";

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_explist, container, false);

        List<Map<String, String>> parentList = new ArrayList<Map<String,
                String>>();
        List<List<Map<String, String>>> allChildList = new
                ArrayList<List<Map<String, String>>>();

        ArrayList<Series> seriesList = new ArrayList<Series>();
        ArrayList<Paint> paintList = new ArrayList<Paint>();

        // 親リストの取得
        SeriesDao dao = new SeriesDao(getActivity().getApplicationContext());
        seriesList = dao.selectAll();

        for (Series series : seriesList) {

            Map<String, String> data = new HashMap<String, String>();
            data.put(KEY_TITLE, series.getName());
            parentList.add(data);

            // 子リスト取得
            paintList = dao.selectAll(series.get_id());
            List<Map<String, String>> childList = new ArrayList<Map<String, String>>();

            for (Paint paint : paintList) {

                Map<String, String> childData = new HashMap<String, String>();
                childData.put(KEY_TITLE, paint.getName());
                childList.add(childData);

            }

            allChildList.add(childList);

        }

        SimpleExpandableListAdapter adapter = new SimpleExpandableListAdapter(
                rootView.getContext(),
                parentList,
                android.R.layout.simple_expandable_list_item_1,
                new String[] {
                        KEY_TITLE
                },
                new int[] {
                        android.R.id.text1
                },
                allChildList,
                android.R.layout.simple_expandable_list_item_2,
                new String[] {
                        KEY_TITLE
                },
                new int[] {
                        android.R.id.text1
                }
                );

        ExpandableListView lv = (ExpandableListView) rootView
                .findViewById(R.id.expandableListView1);
        lv.setAdapter(adapter);
        lv.setOnChildClickListener(listItem_OnChildClickListener);

        return rootView;
    }

    /**
     * ListItem クリック時の処理
     */
    private ExpandableListView.OnChildClickListener listItem_OnChildClickListener = new ExpandableListView.OnChildClickListener() {

        @Override
        public boolean onChildClick(ExpandableListView parent, View v, int groupPosition,
                int childPosition, long id) {

            ExpandableListAdapter adapter = parent.getExpandableListAdapter();

            // Map<String, String> groupItem = (Map<String, String>) adapter
            // .getGroup(groupPosition);
            Map<String, String> childItem = (Map<String, String>)
                    adapter.getChild(
                            groupPosition, childPosition);

            // 登録用ダイアログ呼び出し
            FragmentManager manager = getActivity().getSupportFragmentManager();
            EditDialogFragment dialogFragment = new EditDialogFragment();
            Bundle args = new Bundle();
            args.putString(EditDialogFragment.KEY_PARAM_NAME, childItem.get(KEY_TITLE));
            dialogFragment.setArguments(args);
            dialogFragment.show(manager, "dialog");

            return false;

        }
    };

}
